/*********************************************************
 * copyright xinbohit.com 版权所有 
 * 开发人员：IvanYu
 * 创建时间：2014/10/24 22:36:56
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.WebSite.Admin
{
    /// <summary>
    /// ArticleHandler 的摘要说明
    /// </summary>
    public class ArticleHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string falseStr = "{\"result\":false}";
            string trueStr = "{\"result\":true}";

            string operation = context.Request.Params["o"];
            string idStr = context.Request.Params["id"];
            long id;
            if (!long.TryParse(idStr, out id))
            {
                context.Response.Write(falseStr);
            }

            switch (operation)
            {
                case "0":
                    if (Delete(id))
                    {
                        context.Response.Write(trueStr);
                    }
                    else
                    {
                        context.Response.Write(falseStr);
                    }
                    break;                
                default:
                    context.Response.Write(falseStr);
                    break;
            }
        }

        private bool Delete(long id)
        {
            try
            {
                using (ArticleLogic logic = new ArticleLogic())
                {
                    Article article = logic.GetById(id);
                    if (article != null)
                        logic.Delete(article);
                }
                return true;
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                return false;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}